﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using QXBook.Model;
using System.Xml;
using QXBook.Utils;
using System.Text.RegularExpressions;

namespace Web.App_Code
{
    public class Versions
    {
        /// <summary>
        /// 系统版本号
        /// </summary>
        public static int Ver = 352;
        /// <summary>
        /// 系统名称
        /// </summary>      
        public static string SystemName = "TCG CMS";

        /// <summary>
        /// 获得版本好的文字内容
        /// </summary>
        public static string version
        {
            get
            {
                return SystemName + " Version " + GetVerStr(Ver);
            }
        }

        /// <summary>
        /// 根据系统版本号，得到版本号
        /// </summary>
        /// <param name="ver"></param>
        /// <returns></returns>
        public static string GetVerStr(int ver)
        {

            double v1, v2, v3, v4;

            v1 = ver / 1000;
            v2 = ver / 100;
            v3 = ver / 10;
            v4 = ver / 1;

            return Math.Floor(v1).ToString() + "." + Math.Floor(v2).ToString() + "."
                + Math.Floor(v3).ToString() + "(" + Math.Floor(v4).ToString() + ")";
        }

        /// <summary>
        /// 程序作者
        /// </summary>
        public static string Author
        {
            get
            {
                return "≮三云鬼≯ QQ:644139466,Email:sanyungui@vip.qq.com";
            }
        }

        /// <summary>
        /// 程序官方网站
        /// </summary>
        public static string WebSite
        {
            get
            {
                return "http://www.tcgcms.cn";
            }
        }

        /// <summary>
        /// 下一版本对象
        /// </summary>
        public static VersionItem HigherVersion
        {
            get
            {
                List<VersionItem> vers = WebVersionHistory;
                if (vers == null) return null;
                if (vers.Count == 0) return null;
                int small = -1;
                int tempver = Ver;
                for (int i = 0; i < vers.Count; i++)
                {
                    if (Ver < vers[i].Ver)
                    {
                        if (small == 0)
                        {
                            small = i;
                            tempver = vers[i].Ver;
                        }
                        else
                        {
                            if (vers[i].Ver < tempver)
                            {
                                tempver = vers[i].Ver;
                            }
                        }
                    }
                }
                return small == -1 ? null : vers[small];
            }
        }

        /// <summary>
        /// 获得所有版本号
        /// </summary>
        public static List<VersionItem> WebVersionHistory
        {
            get
            {
                XmlDocument document1 = new XmlDocument();
                document1.Load(WebSite + @"/update/Version.xml");
                XmlNodeList nodes = document1.GetElementsByTagName("Item");
                if (nodes.Count == 0) return null;
                List<VersionItem> ver = new List<VersionItem>();
                foreach (XmlElement element1 in nodes)
                {
                    VersionItem version = new VersionItem();
                    version.Text = element1.SelectSingleNode("Text").InnerText;
                    version.Ver = CommonUtils.ToInt(element1.SelectSingleNode("Ver").InnerText);
                    version.Date = CommonUtils.ToTime(element1.SelectSingleNode("Date").InnerText);
                    version.LogUrl = element1.SelectSingleNode("LogUrl").InnerText;
                    version.Sqls = CommonUtils.ToInt(element1.SelectSingleNode("Sqls").InnerText);
                    ver.Add(version);
                }
                return ver;
            }
        }

        ///// <summary>
        ///// 执行SQL更新语句
        ///// </summary>
        ///// <param name="conn"></param>
        ///// <param name="num"></param>
        ///// <param name="SqlText"></param>
        ///// <returns></returns>
        //public static bool RunSqlSep(Connection conn, int num, ref string SqlText)
        //{
        //    string sqltxtPath = WebSite + "/update/" + GetVerStr(HigherVersion.Ver) + "/sql/" + num.ToString() + ".sql";
        //    string sql = TxtReader.GetRequestText(sqltxtPath, "gb2312");
        //    if (string.IsNullOrEmpty(sql)) return false;

        //    string patten = @"\/\*\*\*(.*?)\$\[(.*?)\]\*\*\*\/";
        //    Match mt = Regex.Match(sql, patten, RegexOptions.Singleline);
        //    if (mt.Success)
        //    {
        //        SqlText = mt.Result("$1");
        //    }
        //    else
        //    {
        //        SqlText = "未检测到更新SQL";
        //        return false;
        //    }
        //    mt = null;

        //    try
        //    {
        //        conn.Execute(sql);
        //    }
        //    catch
        //    {
        //        return false;
        //    }

        //    return true;
        //}
    }
}